<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header">
    <style>
    </style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-show-daily">
    <div class="modal-dialog" style="width:1350px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">查看&nbsp;&nbsp;&nbsp;&nbsp;<span id="projectInfo" style="font-size: 15px"></span>
                </h4>
            </div>
            <div class="modal-body">
                <form id="queryDailyForm">
                    <div class="row">
                        <div class="form-inline">
                            <div id="name" class="form-group" style="margin-right: 10px;margin-left: 10px;">
                                <label>名称</label>
                                <input type="text" id="projectCode" name="projectCode" class="form-control"
                                       placeholder="请输入项目名称或代码" autocomplete="off">
                            </div>
                            <div id="factory" class="form-group" style="margin-right: 10px;margin-left: 10px;">
                                <label>厂名</label>
                                <select id="factoryIdQuery" class="form-control" name="factoryId" title="">
                                    <option value="" selected="selected">请选择</option>
                                    <option th:each="factory:${factories}" th:value="${factory.id}"
                                            th:text="${factory.name}"></option>
                                </select>
                            </div>
                            <div class="form-group" style="margin-left: 10px;margin-right: 10px;">
                                <label>日期</label>
                                <input type="text" class="form-control datepicker" name="inputTime"
                                       readonly="readonly"/>
                            </div>
                            <div id="status" class="form-group" style="margin-right: 10px;">
                                <label>项目状态</label>
                                <select id="statusQuery" name="status" class="form-control">

                                    <option value="-1">请选择</option>
                                    <option value="2" selected="selected">未完成</option>
                                    <option value="3">已完成</option>
                                </select>
                            </div>

                            <button type="button" class="btn btn-success" id="btn-searchDaily">
                                <!--<i class="fa fa-search"></i>-->
                                <span class="Bold">搜索</span>
                            </button>
                        </div>
                    </div>
                    <div id="yearDiv" class="row" style="margin-top: 10px;">
                        <div class="form-inline">
                            <div class="form-group" style="margin-left: 10px;margin-right: 10px;">
                                <label>年度</label>
                                <select id="year2" name="year" class="form-control">
                                    <option value="" selected="selected">请选择</option>
                                </select>
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>任务下达时间区间</label>
                                <input type="text" class="form-control datepicker" name="startTime" style="width: 85px;"
                                       readonly="readonly"/>
                                <span>至</span>
                                <input type="text" class="form-control datepicker" name="endTime" style="width: 85px;"
                                       readonly="readonly"/>
                            </div>
                        </div>
                    </div>
                </form>
                <table id="bootstrap-table1"></table>
            </div>

        </div>
    </div>
</div>
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-total-count">
    <div class="modal-dialog" style="width:1350px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">查看</h4>
            </div>
            <div class="modal-body">
                <form id="queryTotalCountForm">
                    <div class="row">
                        <div class="form-inline">
                            <div class="form-group" style="margin-left:20px;margin-right: 10px;">
                                <label>厂名</label>
                                <select class="form-control" name="factoryId" title="">
                                    <option value="" selected="selected">请选择</option>
                                    <option th:each="factory:${factories}" th:value="${factory.id}"
                                            th:text="${factory.name}"></option>
                                </select>
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>从</label>
                                <input type="text" class="form-control datepicker" name="startTime" style="width: 85px;"
                                       readonly="readonly"/>
                                <span>至</span>
                                <input type="text" class="form-control datepicker" name="endTime" style="width: 85px;"
                                       readonly="readonly"/>
                            </div>
                            <button type="button" class="btn btn-success" id="btn-search-total-count">
                                <!--<i class="fa fa-search"></i>-->
                                <span class="Bold">搜索</span>
                            </button>
                        </div>
                    </div>
                </form>
                <table id="bootstrap-table2"></table>
            </div>

        </div>
    </div>
</div>
<section class="content-header">
    <h1>生产日报查询</h1>
</section>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>名称</label>
                                    <input type="text" name="name" class="form-control" placeholder="请输入项目名称或订单号或代码">
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>生产厂</label>
                                    <select name="factories" class="form-control">
                                        <option value="" selected="selected">请选择</option>
                                        <option th:each="factory:${factories}" th:value="${factory.name}"
                                                th:text="${factory.name}"></option>
                                    </select>
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>项目状态</label>
                                    <select id="top-status" name="status" class="form-control">
                                        <option value="-1">请选择</option>
                                        <option value="2" selected="selected">未完成</option>
                                        <option value="3">已完成</option>
                                    </select>
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>年度</label>
                                    <select id="year" name="year" class="form-control">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>任务下达时间</label>
                                    <input type="text" class="form-control datepicker" name="startTime"
                                           style="width: 85px;" readonly="readonly"/>
                                    <span>至</span>
                                    <input type="text" class="form-control datepicker" name="endTime"
                                           style="width: 85px;" readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <!--<i class="fa fa-search"></i>-->
                                    <span class="Bold">搜索</span>
                                </button>

                                <a href="javascript:void(0);" class="btn btn-info pull-right"
                                   onclick="openDailyList('')" style="text-align: right">
                                    <!--<i class="fa fa-"></i>-->
                                    <span class="Bold">日报明细查询</span>
                                </a>

                                <a href="javascript:void(0);" class="btn btn-info pull-right"
                                   onclick="openTotalCount()" style="margin-right: 10px;">
                                    <!--<i class="fa fa-"></i>-->
                                    <span class="Bold">总产量统计</span>
                                </a>

                            </div>
                        </div>

                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    /** 表格搜索 */
    $("#btn-searchDaily").click(function () {
        js.table.search(table1);
    });

    /**
     * 打开全部日报表
     */
    function openDailyList(code) {

        //隐藏标签
        $('#name').show();
        $('#factory').show();
        $('#status').show();
        $('#yearDiv').show();
        table1 = js.table.init({
            id: "bootstrap-table1",
            queryForm: "queryDailyForm",
            url: ctx + "board/daily/projectDailyList",
            showExport: true,
            columns: [
                {
                    title: '序号', field: 'id', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '代码', field: 'projectCode', width: '80'},
                {title: '项目名称', field: 'projectName', width: '150'},
                {title: '厂名', field: 'factoryName', width: '118'},
                {title: '日期', field: 'inputTime', width: '100'},
                {
                    title: '分类', field: 'orderType', width: '80',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '体系楼';
                        } else if (value === 2) {
                            return '备库';
                        } else if (value === 3) {
                            return '财务';
                        }
                    }
                },
                {title: '自动生产<br>焊接(件)', field: 'autoWeldNum'},
                {title: '自动生产<br>焊接(吨)', field: 'autoWeldWeight',
                    formatter: function (value, row, index) {
                        if(value)
                            return value.toFixed(2);
                    }},
                {title: '手动生产<br>焊接(件)', field: 'manualWeldNum'},
                {title: '手动生产<br>焊接(吨)', field: 'manualWeldWeight',
                    formatter: function (value, row, index) {
                        if(value)
                            return value.toFixed(2);
                    }},
                {title: '生产非<br>焊接(件)', field: 'noWeldNum'},
                {title: '生产非<br>焊接(吨)', field: 'noWeldWeight',
                    formatter: function (value, row, index) {
                        if(value)
                            return value.toFixed(2);
                    }},
                {title: '转储(件)', field: 'storeNum'},
                {title: '转储(吨)', field: 'storeWeight',
                    formatter: function (value, row, index) {
                        if(value)
                            return value.toFixed(2);
                    }},
                {title: '备注', field: 'remark', width: '90'}
                // ,
                // {
                //     title: '操作', width: '65',
                //     formatter: function (value, row, index) {
                //         var actions = [];
                //         actions.push('<a class="btn btn-danger btn-xs" style="margin-bottom: 3px;" href="javascript:void(0)" onclick="delTaskDaily(' + row.id + ')">删除</a> ');
                //         actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEditDailyInputModal(' + row.id + ',\'' + row.projectId + '\')">编辑</a> ');
                //         return actions.join('');
                //     }
                // }
            ]
        });
        js.table.search(table1);
        js.modal.open("modal-show-daily");

    }

    var totalCounttable;

    function openTotalCount() {
        totalCounttable = js.table.init({
            id: "bootstrap-table2",
            queryForm: "queryTotalCountForm",
            url: ctx + "integrate/daily/totalCount",
            columns: [
                {
                    title: '序号', field: 'id', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '厂名', field: 'factoryName', width: '118'},
                {title: '自动生产<br>焊接(件)', field: 'autoWeldNum'},
                {title: '自动生产<br>焊接(吨)', field: 'autoWeldWeight',
                    formatter: function (value, row, index) {
                        if(value)
                        return value.toFixed(2);
                    }
                },
                {title: '手动生产<br>焊接(件)', field: 'manualWeldNum'},
                {title: '手动生产<br>焊接(吨)', field: 'manualWeldWeight',
                    formatter: function (value, row, index) {
                        if(value)
                        return value.toFixed(2);
                    }
                },
                {title: '生产非<br>焊接(件)', field: 'noWeldNum'},
                {title: '生产非<br>焊接(吨)', field: 'noWeldWeight',
                    formatter: function (value, row, index) {
                        if(value)
                        return value.toFixed(2);
                    }
                },
                {title: '转储(件)', field: 'storeNum'},
                {title: '转储(吨)', field: 'storeWeight',
                    formatter: function (value, row, index) {
                        if(value)
                        return value.toFixed(2);
                    }
                }
            ]
        });
        js.modal.open("modal-total-count");
    }

    /**
     * 打开项目对应日报表
     */
    function openProDailyList(code, factories) {
        debugger;
        // 项目信息拼接
        $('#projectCode').val(code);
        $("#factoryIdQuery option").attr("selected",false);
        $("#factoryIdQuery option:contains(" + factories + ")").attr("selected",true);
        $("#statusQuery").val($("#top-status").val());
        //隐藏标签
        $('#name').hide();
        $('#factory').hide();
        $('#status').hide();
        $('#yearDiv').hide();

        table1 = js.table.init({
            id: "bootstrap-table1",
            queryForm: "queryDailyForm",
            url: ctx + "board/daily/projectDailyList",
            showExport: true,
            columns: [
                {
                    title: '序号', field: 'id', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '代码', field: 'projectCode', width: '80'},
                {title: '项目名称', field: 'projectName', width: '150'},
                {title: '厂名', field: 'factoryName', width: '118'},
                {title: '日期', field: 'inputTime', width: '100'},
                {
                    title: '分类', field: 'orderType', width: '80',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '体系楼';
                        } else if (value === 2) {
                            return '备库';
                        } else if (value === 3) {
                            return '财务';
                        }
                    }
                },
                {title: '自动生产<br>焊接(件)', field: 'autoWeldNum'},
                {title: '自动生产<br>焊接(吨)', field: 'autoWeldWeight',formatter: function (value, row, index) {
                        if(value != null){return value.toFixed(2);}else{return ''}
                    }},
                {title: '手动生产<br>焊接(件)', field: 'manualWeldNum'},
                {title: '手动生产<br>焊接(吨)', field: 'manualWeldWeight',formatter: function (value, row, index) {
                        if(value != null){return value.toFixed(2);}else{return ''}
                    }},
                {title: '生产非<br>焊接(件)', field: 'noWeldNum'},
                {title: '生产非<br>焊接(吨)', field: 'noWeldWeight',formatter: function (value, row, index) {
                        if(value != null){return value.toFixed(2);}else{return ''}
                    }},
                {title: '转储(件)', field: 'storeNum'},
                {title: '转储(吨)', field: 'storeWeight',formatter: function (value, row, index) {
                        if(value != null){return value.toFixed(2);}else{return ''}
                    }},
                {title: '备注', field: 'remark', width: '90'}
                // ,
                // {
                //     title: '操作', width: '65',
                //     formatter: function (value, row, index) {
                //         var actions = [];
                //         actions.push('<a class="btn btn-danger btn-xs" style="margin-bottom: 3px;" href="javascript:void(0)" onclick="delTaskDaily(' + row.id + ')">删除</a> ');
                //         actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEditDailyInputModal(' + row.id + ',\'' + row.projectId + '\')">编辑</a> ');
                //         return actions.join('');
                //     }
                // }
            ]
        });
        js.table.search(table1);
        js.modal.open("modal-show-daily");

    }

    function yearSelectCreate() {
        var date = new Date();
        var endYear = date.getFullYear();
        for (var i = endYear; i >= 2016; i--) {
            if (i === endYear) {
                $("#year").append("<option value='" + i + "' selected='selected'>" + i + "年" + "</option>");
            }
            else if (i === 2016) {
                $("#year").append("<option value='" + i + "'>" + i + "年及以前" + "</option>");
                $("#year2").append("<option value='" + i + "'>" + i + "年及以前" + "</option>");
            } else {
                $("#year").append("<option value='" + i + "'>" + i + "年" + "</option>");
                $("#year2").append("<option value='" + i + "'>" + i + "年" + "</option>");
            }

        }
    }

    var beforeField;
    var beforeOrderNO = 0;
    var falseTotal = 0;


    $(function () {
        yearSelectCreate();
        /**
         * 加载列表
         */
        table = js.table.init({
            url: ctx + "integrate/daily/list",
            showColumns: true,
            showExport: true,
            sortable: true,                                    // 是否启用排序
            sortStable: true,                                  // 设置为 true 将获得稳定的排序
            responseHandler: function (res) {
                falseTotal = res.data.falseTotal
                return {
                    'total': res.data.total,
                    'rows': res.data.rows
                }
            },
            onLoadSuccess: function (data) {
                //合并单元格
                mergeCells(data.rows, "orderNO", 1, $('#bootstrap-table'));
                mergeCells(data.rows, "name", 1, $('#bootstrap-table'));
                mergeCells(data.rows, "orderNum", 1, $('#bootstrap-table'));
                //更改分页信息
                $('.pagination-info').html("共  " + falseTotal + " 条记录");
            },
            rowStyle: function (row, index) {
                // if (row.num < 0 ||
                //     row.weight < 0 ||
                //     row.produceNum < 0 ||
                //     row.produceWeight < 0 ||
                //     row.storeNum < 0 ||
                //     row.storeWeight < 0 ||
                //     row.num - row.storeNum < 0 ||
                //     row.weight - row.storeWeight < 0
                // ) {
                //     return {
                //         css: {
                //             background: '#FF99CC'
                //         }
                //     }
                // } else {
                //     return {
                //         css: {
                //             background: '#FFFFFF'
                //         }
                //     }
                // }
                return {
                    css: {
                        background: '#FFFFFF',
                        border: '#DDDDDD 1px solid'
                    }
                }
            },
            columns: [
                {
                    title: '序号', field: 'orderNO', width: '40',
                    formatter: function (value, row, index) {
                        if (index === 0) {
                            beforeOrderNO = index;
                            beforeField = "";
                        }
                        if (beforeField != value) {
                            beforeField = value;
                            beforeOrderNO = beforeOrderNO + 1;
                        }
                        return beforeOrderNO;
                    }
                },
                {title: '项目名称', field: 'name', width: '235'},
                {title: '订单号', field: 'orderNum', width: '80'},
                {
                    title: '客户要求<br>发货日期', field: 'shipTime', width: '100',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (row.topic === 1) {
                            return `<span class="text-red">${value}</span>`;
                        }
                        return value;
                    }
                },
                // {title: '代码', field: 'code', width: '80'},
                {title: '生产厂', field: 'factories', width: '120'},
                {title: '下计划<br>时间', field: 'sendTime', width: '100'},
                {
                    title: '下计划<br>数量', field: 'num', formatter: function (value, row, index) {
                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value;
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: '下计划<br>重量', field: 'weight', formatter: function (value, row, index) {
                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value;
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: '生产数量', field: 'produceNum', formatter: function (value, row, index) {
                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value;
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: '生产重量', field: 'produceWeight', formatter: function (value, row, index) {
                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value;
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: '转储数量', field: 'storeNum', formatter: function (value, row, index) {
                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value;
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: '转储重量', field: 'storeWeight', formatter: function (value, row, index) {
                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value;
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: '剩余数量', field: 'num',
                    formatter: function (value, row, index) {
                        value = value - row.storeNum;
                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value;
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: '剩余重量', field: 'weight',
                    formatter: function (value, row, index) {
                        if (value && row.storeWeight) {
                            value = value.toFixed(2) - row.storeWeight.toFixed(2);
                        }else{
                            value = value - row.storeWeight;
                        }

                        if (value != null || value != undefined) {
                            value = String(value).replace(/^(.*\..{2}).*$/, "$1");
                            value = Number(value); // number = 12.3321
                            if (String(value).indexOf('.') > 0 && String(value).substring(String(value).indexOf('.')).length > 4) {
                                value = Number(value).toFixed(2);
                            }
                            return value < 0 ? '' : value;
                        } else {
                            return value < 0 ? '' : value;
                        }
                    }
                },
                {title: '转储<br>完成时间', field: 'storeCompleteTime', width: '100'},
                {
                    title: '操作', width: '40',
                    formatter: function (value, row) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs" target="_blank" href="' + ctx + 'board/single/' + row.id + '"> 项目</a> ');
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="openProDailyList(\'' + row.code + '\',\'' + row.factories + '\')"> 明细</a> ');
                        return actions.join('');
                    }
                }
            ]
        });


        /**
         * 表格搜索
         */
        $("#btn-search").click(function () {
            js.table.search(table);
        });

        /**
         * 表格搜索
         */
        $("#btn-search-total-count").click(function () {
            js.table.search(totalCounttable);
        });
    });
    var rowSpanNumList;

    /**
     * 合并单元格
     * @param data  原始数据（在服务端完成排序）
     * @param fieldName 合并属性名称
     * @param colspan   合并列
     * @param target    目标表格对象
     */
    function mergeCells(data, fieldName, colspan, target) {
        rowSpanNumList = new Array();
        for (var i = 0; i < data.length; i++) {
            for (var prop in data[i]) {
                if (prop == fieldName) {
                    var isHave = false;
                    if (rowSpanNumList.length > 0) {
                        for (var j = 0; j < rowSpanNumList.length; j++) {
                            if (rowSpanNumList[j].key == data[i][prop]) {
                                isHave = true;
                                rowSpanNumList[j].val = rowSpanNumList[j].val + 1;
                            }
                        }
                    }
                    if (isHave == false) {
                        var rowSpanNumObj = new Object();
                        rowSpanNumObj.key = data[i][prop];
                        rowSpanNumObj.val = 1;
                        rowSpanNumList.push(rowSpanNumObj);
                    }
                    break;
                }
            }
        }

        var index = 0;
        for (var item = 0; item < rowSpanNumList.length; item++) {
            var count = rowSpanNumList[item].val;
            $(target).bootstrapTable('mergeCells', {index: index, field: fieldName, colspan: colspan, rowspan: count});
            index += count;
        }
    }


</script>
</body>
</html>
